
package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Region;

@Transactional(readOnly = false)
@Repository(value = "regionDao")
public class RegionDaoImpl implements RegionDao {

	private EntityManager em = null;
    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }
	@SuppressWarnings("unchecked")
	public List<Region> getRegionList(short idCountry) {
		Query  q = em.createNamedQuery("Region.findByCountryId");
    	q.setParameter("countryId",idCountry);
    	return q.getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Region> getRegionList(short idCountry,boolean active){
		Query  q = em.createNamedQuery("Region.findByRegionIdActive");
    	q.setParameter("countryId",idCountry);
    	q.setParameter("active",active);
    	q.setParameter("active2",true);
    	return q.getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Region> getRegionList() {
		return em.createNamedQuery("Region.findAll").getResultList();
	}
	public void add(Region region) {
		em.persist(region);
	}
	public void update(Region region) {
		em.merge(region);
	}

	public void remove(Region region) {
		//Query q = em.createNamedQuery("Region.deleteByCountryIdByRegionId");
    	//q.executeUpdate();
		em.remove(em.getReference(Region.class,region.getRegionId()));
    	
	}

	public Region findRegionByRegionId(Integer regionId) {
		Region res=null;
		try {
			res=em.find(Region.class,regionId);
		} catch (Exception e) {
			// TODO: handle exception
		}
    	return res;
	}

}

